package com.ruoyi.tasks.service.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.system.api.HouseholdService;
import com.ruoyi.system.api.domain.Household;
import com.ruoyi.tasks.domain.HouseholdTasks;
import com.ruoyi.tasks.dto.BuildingTasksDto;
import com.ruoyi.tasks.mapper.HouseholdTasksMapper;
import org.apache.http.nio.util.SharedOutputBuffer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.ruoyi.tasks.mapper.BuildingTasksMapper;
import com.ruoyi.tasks.domain.BuildingTasks;
import com.ruoyi.tasks.service.IBuildingTasksService;

/**
 * 楼栋核酸任务CRUDService业务层处理
 *
 * @author ruoyi
 * @date 2022-10-05
 */
@Service
public class BuildingTasksServiceImpl implements IBuildingTasksService
{
    @Autowired
    private BuildingTasksMapper buildingTasksMapper;

    @Autowired
    private HouseholdService householdService;

    @Autowired
    private HouseholdTasksMapper householdTasksMapper;

    /**
     * 查询楼栋核酸任务CRUD
     *
     * @param id 楼栋核酸任务CRUD主键
     * @return 楼栋核酸任务CRUD
     */
    @Override
    public BuildingTasks selectBuildingTasksById(Long id)
    {
        BuildingTasks buildingTasks = buildingTasksMapper.selectBuildingTasksById(id);
        String rqName = buildingTasks.getRqName();
        List<Household> household = householdService.getHousehold(rqName);
        household.forEach(households -> {
            String householdPhone = households.getHouseholdPhone();
            System.out.println("核酸任务即将开始，请各位住户做好准备！！");
        });
        return null;
    }

    /**
     * 查询楼栋核酸任务CRUD列表
     *
     * @param buildingTasks 楼栋核酸任务CRUD
     * @return 楼栋核酸任务CRUD
     */
    @Override
    public List<BuildingTasks> selectBuildingTasksList(BuildingTasks buildingTasks)
    {
        return buildingTasksMapper.selectBuildingTasksList(buildingTasks);
    }

    /**
     * 新增楼栋核酸任务CRUD
     *
     * @param dto 楼栋核酸任务CRUD
     * @return 结果
     */
    @Override
    public int insertBuildingTasks(BuildingTasksDto dto)
    {
        Date date = new Date();
        if(date.before(dto.getStartTime())){
            dto.setState("0");
        } else if (date.before(dto.getEndTime())) {
            dto.setState("1");
        } else {
            dto.setState("2");
        }
        dto.setTestingNumber(4);
        dto.setCompleteNumber(0);
        int i = buildingTasksMapper.insertBuildingTasks(dto);


        HouseholdTasks householdTasks = new HouseholdTasks();
        List<Household> household = householdService.getHousehold(dto.getRqName());
        household.forEach(households -> {
            String householdName = households.getHouseholdName();
            householdTasks.setTasks(dto.getRqName() + dto.getBuildingName() + householdName + dto.getTasks());
            householdTasks.setState("0");
            householdTasks.setHouseholdName(householdName);
            householdTasks.setStartTime(dto.getStartTime());
            householdTasks.setEndTime(dto.getEndTime());
            householdTasks.setPosition(dto.getPosition());
            householdTasksMapper.insertHouseholdTasks(householdTasks);
        });



        extracted(dto, date);

        return i;
    }

    @Scheduled(cron = "0 */1 * * * ?")
    private void extracted(BuildingTasksDto dto, Date date) {
        Calendar instance = Calendar.getInstance();
        if(dto.getRadio().equals("1")){
            instance.setTime(dto.getStartTime());
            instance.add(Calendar.HOUR, -1);
            Date time = instance.getTime();
            System.out.println(time);
            if(date.equals(time)){
                selectBuildingTasksById(dto.getId());
            }
        } else {
            instance.setTime(dto.getEndTime());
            instance.add(Calendar.HOUR, +1);
            Date time = instance.getTime();
            if(date.equals(time)){
                selectBuildingTasksById(dto.getId());
            }
        }
    }

    /**
     * 修改楼栋核酸任务CRUD
     *
     * @param buildingTasks 楼栋核酸任务CRUD
     * @return 结果
     */
    @Override
    public int updateBuildingTasks(BuildingTasks buildingTasks)
    {
        return buildingTasksMapper.updateBuildingTasks(buildingTasks);
    }

    /**
     * 批量删除楼栋核酸任务CRUD
     *
     * @param ids 需要删除的楼栋核酸任务CRUD主键
     * @return 结果
     */
    @Override
    public int deleteBuildingTasksByIds(Long[] ids)
    {
        return buildingTasksMapper.deleteBuildingTasksByIds(ids);
    }

    /**
     * 删除楼栋核酸任务CRUD信息
     *
     * @param id 楼栋核酸任务CRUD主键
     * @return 结果
     */
    @Override
    public int deleteBuildingTasksById(Long id)
    {
        return buildingTasksMapper.deleteBuildingTasksById(id);
    }
}
